package com.educate.common.utils.converter;

import org.springframework.beans.BeanUtils;
import org.springframework.core.convert.converter.Converter;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;


@SuppressWarnings("unchecked")
public abstract class CopyPropertiesConverter<S, T> implements Converter<S, T> {

    private final Class<T> tClass;

    public CopyPropertiesConverter() {
        Type superclass = this.getClass().getGenericSuperclass();
        tClass = (Class<T>) ((ParameterizedType)superclass).getActualTypeArguments()[1];
    }

    @Override
    public T convert(S s) {
        T target = BeanUtils.instantiate(tClass);
        BeanUtils.copyProperties(s, target);
        return target;
    }
}
